import 'package:get/get.dart';

/// 自动销毁 [Worker]
///
/// 在 [onClose] 会自动进行销毁
mixin WorkerAutoDisposeMixin on GetxController {
  late final Workers _autoDisposeWorkers = Workers([]);

  /// 加入到列表，在 [onClose] 会自动进行销毁
  void addAutoDisposeWorker(Worker worker) {
    _autoDisposeWorkers.workers.add(worker);
  }

  @override
  void onClose() {
    _autoDisposeWorkers.dispose();
    super.onClose();
  }
}

extension WorkerExtension on Worker {
  void autoDispose(WorkerAutoDisposeMixin manager) {
    manager.addAutoDisposeWorker(this);
  }
}
